package com.wanxi.manage;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagement {
    public static void main(String[] args) {
        /**
         * 1.对象
         * 2.死循环
         * 3.有很多功能：不同的方法
         * 4.容器：集合
         */
        Scanner scanner = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            System.out.println("--------欢迎来到学生管理系统-----------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查询所有学生");
            System.out.println("5 退出");
            System.out.println("请输入编号");
            int str = scanner.nextInt();
            switch (str) {
                case 1:
                    addStudent(list);
                    break;
                case 2:
                    deleteStudent(list);
                    break;
                case 3:
                    updateStudent(list);
                    break;
                case 4:
                    findAllStudent(list);
                    break;
                case 5:
                    System.out.println("已退出");
                    System.exit(0);
            }
        }
    }

    private static void findAllStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("没有学生数据");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的学生姓名");
        String name = scanner.next();
        //如果集合里面没有数据，则删除失败
        boolean isHave = false;
        for (Student s : list
        ) {
            if (s.getName().equals(name)) {
                list.remove(s);
                isHave = true;
            }
        }
        if (!isHave) {
            System.out.println("删除失败");
        } else {
            System.out.println("删除成功");
        }
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改的学生姓名");
        String name = scanner.next();
//        forEach没有index索引，所以只能用fori
//        for (Student s:list
//             ) {
//
//        }
        boolean isHave = false;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                isHave = true;
                System.out.println("请输入学生信息，格式：姓名,年龄");
                String str = scanner.next();//张三,23
                Student student = new Student();
                student.setName(str.split(",")[0]);
                student.setAge(Integer.parseInt(str.split(",")[1]));
                list.set(i, student);
            }
        }
        if (!isHave) {
            System.out.println("没有找到你要修改的学生信息 ");
        } else {
            System.out.println("修改成功");
        }

    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生信息，格式：姓名,年龄");
        String str = scanner.next();//张三,23
        Student student = new Student();
        student.setName(str.split(",")[0]);
        student.setAge(Integer.parseInt(str.split(",")[1]));
        list.add(student);
        System.out.println("新增成功");
    }
}
